package com.github.postsanf.yinian.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.ShowBackup;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.LinkedHashMap;
import java.util.List;

public class GuideImportActivity extends BaseActivity {
    private Toolbar mToolbar;
    private ImageView iv_import_instance;
    private ProgressDialog mProgressDialog;
    private ImageView iv_no_import_pic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_import);
        initView();
        showBackup();
    }

    private void initView() {
        mToolbar = ((Toolbar) findViewById(R.id.tl_return));
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);
        iv_import_instance = ((ImageView) findViewById(R.id.iv_import_instance));
        iv_no_import_pic = ((ImageView) findViewById(R.id.iv_no_import_pic));
        iv_import_instance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(GuideImportActivity.this, LocalAlbumImportActivity.class));
                finish();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    // 显示备份照片
    private void showBackup() {
        mProgressDialog = ProgressDialog.show(this, "", "");
        String urlsto = getString(R.string.BASE_URL) + getString(R.string.showBackupPhotos);
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, application.getCurID() + "");
        mParam.put(CommonConstants.SIGN, "initialize");
        StringRequest req = new StringRequest(urlsto)
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        ShowBackup showBackup = gson.fromJson(s, ShowBackup.class);
                        if (TextUtils.equals(showBackup.getMsg(), "success")) {
                            List<ShowBackup.DataBean> data = showBackup.getData();
                            if (data.size() != 0) {
                                Intent intent = new Intent(GuideImportActivity.this, ImporProgressActivity.class);
                                intent.putExtra("showBackup", s);
                                startActivity(intent);
                                finish();
                                iv_no_import_pic.setVisibility(View.GONE);
                                iv_import_instance.setVisibility(View.GONE);
                            }else{
                                iv_no_import_pic.setVisibility(View.VISIBLE);
                                iv_import_instance.setVisibility(View.VISIBLE);
                            }

                        } else {
                            Log.d("yuhao", "UploadToYiNian=" + "fail");
                        }
                        mProgressDialog.dismiss();
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        mProgressDialog.dismiss();
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }


    private long fristTime;

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
        fristTime = System.currentTimeMillis();
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
        Intent intent = new Intent(ACTIONs.record);
        intent.putExtra("key", "私人相册_3");
        intent.putExtra("value", new Long(System.currentTimeMillis() - fristTime).intValue());
        sendBroadcast(intent);
    }
}
